using System;
using System.Collections.Generic;
using System.Text;
using agsXMPP.protocol.client;

namespace SwinMessengerLibrary
{
    public class Roster
    {
        private List<Contact> _Contacts;
        public List<Contact> Contacts
        {
            get { return _Contacts; }
        }
        private Server _Parent;

        public Server Parent
        {
            get { return _Parent; }
        }

        public event EventHandler OnRosterItem;

        public Roster(Server parent)
        {
            _Contacts = new List<Contact>();
            _Parent = parent;
        }

        public void Chat(string fromID, string message, string state)
        {

            Contact contact = FindContact(fromID);
            contact.Chat(message, state);

            if (OnMessage != null)
            {
                MessageEventArgs mea = new MessageEventArgs(fromID, message, state);
                OnMessage(this, mea);
            }
        }

        public void SendTo(string toID, string message)
        {
            _Parent.SentTo(toID, message);
        }

        public void UpdateContact(Contact contact)
        {
            foreach (Contact c in _Contacts)
            {
                if (c.Id.Equals(contact.Id))
                {
                    c.Update(contact);
                    EventArgs e = new ContactEventArgs(contact);

                    if (OnRosterItem != null)
                    {
                        OnRosterItem(this, e);
                    }
                }
            }
        }

        public void UpdateContact(string id, ContactStatus status)
        {
            foreach (Contact c in _Contacts)
            {
                if (c.Id.ToLower().Equals(id.ToLower()))
                {
                    c.Status = status;
                    EventArgs e = new ContactEventArgs(c);

                    if (OnRosterItem != null)
                    {
                        OnRosterItem(this, e);
                    }
                }
            }
        }

        public Boolean Closing()
        {
            foreach (Contact contact in _Contacts)
            {
                if (!contact.CanClose)
                {
                    return false;
                }
            }
            return true;
        }

        public void Close()
        {
            foreach (Contact contact in _Contacts)
            {
                contact.Close();
            }
        }

        public void Add(Contact contact)
        {
            _Contacts.Add(contact);

            ContactEventArgs e = new ContactEventArgs(contact);

            if (OnRosterItem != null)
            {
                OnRosterItem(this, e);
            }
        }

        public Contact FindContact(string person)
        {
            foreach (Contact contact in _Contacts)
            {
                if (contact.Id.ToLower() == person.ToLower())
                {
                    return contact;
                }
            }
            return null;
        }

        public event EventHandler OnMessage;
    }
}
